<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars 
define("PRIMARY_KEY","nokwit");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_approval_receipt.php";
$FileTemplate  = "ais_approval_receipt_list.html";
$TableName     = "ais_receipt";
$SubHeaderTitle= "Approval Receipt";
$PageSize      = 10 ;
$HTMLGridList  = "MemorialGrid";
//************ FORM ACTION **************
	if (GetParam("Action","")=="Delete") {
		DeleteRecord ("txtCheck",$PageSize);
	}elseif (GetParam("Action","")=="Approval") {
		ApprovalRecord ("txtCheck",$PageSize);
	}

//Initiate Database Table For Search;
$FCode = GetParam("nomemoestSearch","");


//******* DATABASE FORM QUERY ***********
$SQL = "SELECT *,SUM(jumlah) as jumlah FROM `$TableName` ";
$SQLCount = "SELECT count(*) FROM `$TableName` ";
$WHERE  = "ktrasl='".GetOfficeID()."' ";
$WHERE  .= " AND nokwit like 'KW%' ";
$WHERE  .= " AND ".QueryLedger()." AND app1<>1 ";

//Retrieve Parameter from search

if ($FCode) {
	$CustomWhere = " nokwit like '".$FCode."%'";
	if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
	else $WHERE = $CustomWhere;	
}

if (strlen($WHERE)) {
	$SQL = $SQL."WHERE ".$WHERE;
	$SQLCount .= "WHERE ".$WHERE;
}
else $SQL = $SQL;

$SQL .= "GROUP BY nokwit ";
$SQLCount .= "GROUP BY nokwit ";

//Select Customer 
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ; 
if (!$RecCnt) $RecCnt = 0;
if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);

$TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ; 
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;
          
        } 
	for ($x=$StartRecord;$x<=$EndRecord;$x++) {
		if (GetParam($sequence_name.$x,""))	{
			$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}


function ApprovalRecord ($sequence_name,$PageSize) {
	global $DBConnection,$FileName,$TableName;
        if (GetParam("PageNum","")) {
          $StartRecord = GetParam("PageNum","")*$PageSize-$PageSize+1;
          $EndRecord   = $StartRecord+$PageSize-1;
          
        } 
	for ($x=$StartRecord;$x<=$EndRecord;$x++) {
		if (GetParam($sequence_name.$x,""))	{
			SetTransApproval(GetParam("Type",""),GetParam($sequence_name.$x,""));
			//$DBConnection->Delete("$TableName",$sequence_name.$x);
	    }
	}
	header("Location: $FileName");
	exit;	
	
}

function SetTransApproval($appr_level,$nobkt) {
	global $DBConnection;
	if (!ereg("KW",$nobkt)) return;
	if ($appr_level=="Approval1") {
		CheckAndUpdateAppr("Approval1",$nobkt);
	}
}

function CheckAndUpdateAppr($appr_level,$nobkt){
	global $DBConnection,$FileName,$TableName;	
	if ($appr_level=="Approval1") {
		$SQL = "SELECT app1 FROM $TableName WHERE nokwit='$nobkt'";
		$ApprStat = $DBConnection->dbc->get_var($SQL);
		if ($ApprStat<>'1') 
			$SQL = "UPDATE $TableName SET app1='1', nmapp1='".GetStaffName()."' WHERE nokwit = '$nobkt'";
		else return false;

	} 
        
	$DBConnection->dbc->query($SQL);

}


?>